package com.eedi.framework.mail.service;

import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.mail.controller.sys.vo.account.MailAccountPageReq;
import com.eedi.framework.mail.dal.mapper.SysMailAccountMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import com.eedi.framework.mail.controller.sys.vo.account.MailAccountSaveReq;
import javax.annotation.Resource;
import java.util.List;
import com.eedi.framework.mail.dal.dataobject.SysMailAccountDO;
import com.eedi.framework.common.util.object.BeanUtils;

import static com.eedi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.eedi.framework.mail.enums.SysMailErrorCodeConstants.SYS_MAIL_ACCOUNT_NOT_EXISTS;
import static com.eedi.framework.mail.enums.SysMailErrorCodeConstants.SYS_MAIL_ACCOUNT_RELATE_TEMPLATE_EXISTS;

import com.eedi.framework.mail.dal.SysMaillCacheKeyConstants;

/**
 * 邮箱账号 Service 实现类
 *
 * @author zybourn
 * @since 2022-03-21
 */
@Service
@Validated
@Slf4j
public class SysMailAccountServiceImpl implements SysMailAccountService {

    @Resource
    private SysMailAccountMapper sysMailAccountMapper;

    @Resource
    private SysMailTemplateService sysMailTemplateService;

    @Override
    public String createMailAccount(MailAccountSaveReq createReqVO) {
        SysMailAccountDO account = BeanUtils.toBean(createReqVO, SysMailAccountDO.class);
        sysMailAccountMapper.insert(account);
        return account.getSysMailAccountId();
    }

    @Override
    @CacheEvict(value = SysMaillCacheKeyConstants.MAIL_ACCOUNT, key = "#updateReqVO.id")
    public void updateMailAccount(MailAccountSaveReq updateReqVO) {
        // 校验是否存在
        validateMailAccountExists(updateReqVO.getId());

        // 更新
        SysMailAccountDO updateObj = BeanUtils.toBean(updateReqVO, SysMailAccountDO.class);
        sysMailAccountMapper.updateById(updateObj);
    }

    @Override
    @CacheEvict(value = SysMaillCacheKeyConstants.MAIL_ACCOUNT, key = "#id")
    public void deleteMailAccount(String id) {
        // 校验是否存在账号
        validateMailAccountExists(id);
        // 校验是否存在关联模版
        if (sysMailTemplateService.getMailTemplateCountByAccountId(id) > 0) {
            throw exception(SYS_MAIL_ACCOUNT_RELATE_TEMPLATE_EXISTS);
        }

        // 删除
        sysMailAccountMapper.deleteById(id);
    }

    private void validateMailAccountExists(String id) {
        if (sysMailAccountMapper.selectById(id) == null) {
            throw exception(SYS_MAIL_ACCOUNT_NOT_EXISTS);
        }
    }

    @Override
    public SysMailAccountDO getMailAccount(String id) {
        return sysMailAccountMapper.selectById(id);
    }

    @Override
    @Cacheable(value = SysMaillCacheKeyConstants.MAIL_ACCOUNT, key = "#id", unless = "#result == null")
    public SysMailAccountDO getMailAccountFromCache(String id) {
        return getMailAccount(id);
    }

    @Override
    public PageResult<SysMailAccountDO> getMailAccountPage(MailAccountPageReq pageReqVO) {
        return sysMailAccountMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SysMailAccountDO> getMailAccountList() {
        return sysMailAccountMapper.selectList();
    }

}
